package com.mingqijia.gassafety.console.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gap.basic.base.CommonResponse;
import com.gap.basic.exception.BizException;
import com.github.pagehelper.util.StringUtil;
import com.mingqijia.gassafety.db.entity.Dtu;
import com.mingqijia.gassafety.db.entity.vo.ScreenSmallBellItemVO;
import com.mingqijia.gassafety.db.mapper.DtuMapper;
import com.mingqijia.gassafety.shared.utils.HeaderUtil;
import com.mingqijia.gassafety.shared.utils.commonUtils.PageUtil;
import com.mingqijia.gassafety.webserver.request.CustDetailRequest;
import com.mingqijia.gassafety.webserver.request.ScreenSmallBellListRequest;
import com.mingqijia.gassafety.webserver.response.ConsumerDetailResponse;
import com.mingqijia.gassafety.webserver.service.CockpitService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.mingqijia.gassafety.shared.constant.Constants.BIND_CHANNEL_ASST;
import static com.mingqijia.gassafety.shared.constant.Constants.BIND_CHANNEL_GAS;

/**
 * @program: gassafety
 * @description:
 * @author: Mr.Wang
 * @create: 2022-12-12 10:06
 **/


@Slf4j
@Api(tags = "驾驶舱")
@RestController(value = "CockpitController")
@RequestMapping("/business/v1/cockpit")
public class CockpitController {

    @Autowired
    HeaderUtil headerUtil;
    @Autowired
    DtuMapper dMapper;
    @Autowired
    CockpitService cockpitService;

    @ApiOperation(value = "客户地图打点详情")
    @PostMapping("/consumerDetail")
    public CommonResponse<ConsumerDetailResponse> consumerDetail(@RequestBody @Valid CustDetailRequest request) {
        request.setTemplateType(headerUtil.getTemplateType());
        log.info("consumerDetail--request:{}", JSONObject.toJSONString(request));
        ConsumerDetailResponse response = new ConsumerDetailResponse();
        //查询设备来源
        QueryWrapper<Dtu> query = new QueryWrapper<>();
        query.lambda().or().eq(Dtu::getIsDeleted, 0)
                .eq(Dtu::getConsumerId, request.getConsumerId());
        List<Dtu> dtus = dMapper.selectList(query);
        Integer from = 0;
        if (!CollectionUtils.isEmpty(dtus)) {
            from = dtus.get(0).getEquipmentFrom();
        }
        //模板二
        if (BIND_CHANNEL_ASST == request.getTemplateType()) {
            if ((StringUtil.isEmpty(request.getMemberId()) || StringUtil.isEmpty(request.getOrderNo()))
                    &&from!=1    ) {
                throw new BizException("模板二入参缺少用户id或工单号");
            }
            if (from==1){
                response = cockpitService.consumerDetailGas(request);
            }else {
                response = cockpitService.consumerDetailAsst(request);
            }

            // 模板一
        } else if (BIND_CHANNEL_GAS == request.getTemplateType()) {
            response = cockpitService.consumerDetailGas(request);

        }
        return CommonResponse.success(response);
    }

    @ApiOperation(value = "告警信息")
    @GetMapping("/warningInfo")
    public CommonResponse<List<ConsumerDetailResponse>> warningInfo(@RequestParam("applyStatus")Integer applyStatus,
                                                                    @RequestParam(value = "dataType",required = false,defaultValue = "0") Integer dataType,
                                                                    @RequestParam(required = false, value = "timeStart") String timeStart,
                                                                    @RequestParam(required = false, value = "timeEnd") String timeEnd) {
        //dataType：数据类型 0：报警+故障 1报警
        Integer templateType = headerUtil.getTemplateType();
        log.info("warningInfo--templateType:{}", templateType);
        List<ConsumerDetailResponse> response = new ArrayList<>();
        //模板二
        if (BIND_CHANNEL_ASST == templateType) {
            response = cockpitService.warningInfoAsst(templateType,applyStatus);

            // 模板一
        } else if (BIND_CHANNEL_GAS == templateType) {
            response = cockpitService.warningInfoGas(templateType,applyStatus,dataType,timeStart,timeEnd);

        }
        return CommonResponse.success(response);
    }


    /**
     * 设备运行情况
     *
     * @return
     */
    @GetMapping("equipmentRunningInfo")
    @ApiOperation(value = "设备运行情况")
    public CommonResponse<Object> equipmentRunningInfo() {
        return CommonResponse.success(cockpitService.equipmentRunningInfo());
    }

    /**
     * 未处理工单数
     *
     * @return
     */
    @GetMapping("getConditionSum")
    @ApiOperation(value = "未处理工单数")
    public CommonResponse<Object> getConditionSum() {
        return CommonResponse.success(cockpitService.getConditionSum());
    }

    /**
     * 工单情况
     * type=0 今日  type =1 一周
     *
     * @return
     */
    @GetMapping("orderCondition")
    @ApiOperation(value = "工单情况")
    public CommonResponse<Object> orderCondition(
            @RequestParam(name = "startTime", required = false) String startTime,
            @RequestParam(name = "endTime", required = false) String endTime,
            @RequestParam(name = "type") int type) {
        return CommonResponse.success(cockpitService.orderCondition(startTime, endTime, type));
    }

    /**
     * 主模块数据
     *
     * @return
     */
    @GetMapping("primaryData")
    @ApiOperation(value = "主模块数据")
    public CommonResponse<Object> primaryData() {
        return CommonResponse.success(cockpitService.primaryData());
    }

    /**
     * 内部使用
     *
     * @return
     */
    @GetMapping("appealLog")
    @ApiOperation(value = "数据处理")
    public CommonResponse<Object> appealLog(@RequestParam(name = "id", required = false) String id) {
        return CommonResponse.success(cockpitService.appealLog(id));
    }

    /**
     * alarmDtuSum
     *
     * @return
     */
    @GetMapping("/alarmDtuSum")
    @ApiOperation(value = "报警设备数量")
    public CommonResponse<Object> alarmDtuSum() {
        return CommonResponse.success(cockpitService.alarmDtuSum());
    }


    @ApiOperation("大屏报警，故障设备查询接口")
    @RequestMapping(value = "/alarm/list", method = RequestMethod.POST)
    public CommonResponse<PageUtil<ScreenSmallBellItemVO>> getScreenSmallBellList(@RequestBody ScreenSmallBellListRequest request){
        return CommonResponse.success(cockpitService.getScreenSmallBellList(request));
    }

}
